package 二叉搜索树中的插入操作;

/**
 * createTime: 2025/4/7 23:53
 * description:
 *
 * @author AirMan
 */
public class Solution3 {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        // 迭代法
        TreeNode p = root, partent = root;
        if (root == null){
            return new TreeNode(val);
        }
        while (true){
            if (p == null){
                TreeNode child = new TreeNode(val);
                if(partent.val > val){
                    partent.left = child;
                }else{
                    partent.right = child;
                }
                return root;
            }
            partent = p;
            if (p.val > val){
                p = p.left;
            }else{
                p = p.right;
            }
        }
    }
}
